package com.kcnet.qk.controller.addfriend;

import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import  com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.w3c.dom.Node;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;
import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/4/25 0025.
 * 通过好友请求
 */

public class AddFriendAdoptCTR extends BasePeopleController {

    public boolean init_imm;
    public boolean initRefresh;

    private static String TAG = "通过好友请求";

    private static AddFriendAdoptCTR mRongCloudInstance;


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static AddFriendAdoptCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (AddFriendAdoptCTR.class) {
                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new AddFriendAdoptCTR();
                }
            }
        }

        return mRongCloudInstance;
    }




    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击添加好友模块按钮");
            //sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TAB_JHYQQ));
            sleepTimedDefault();
            searchClick();
        }
        else if(!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.DWZ")){
            NLogger.d(TAG, "2、点击搜索新好友");
            searchTypeClick(FBStringUtil.getString(StringConst.ADDFRIEND_SEATCH_NEW_FRIEND));
            initFbMain = true;
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")){
            init_imm = true;
            tagTextClick(FBStringUtil.getString(StringConst.ADDFRIEND_QINGQIU));//FBStringUtil.getString(StringConst.ADDFRIEND_QINGQIU)
            NLogger.d(TAG, "3、点击 -请求");
        }
        else if (init_imm && ( mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            if(!initRefresh){
                NLogger.d(TAG, "4、第一次进来先下啦刷新下数据");
                RootShell.swipe(0,400,0,700);
                sleepTime(2000);
                initRefresh = true;
            }else {
                NLogger.d(TAG, "4、执行 自动通过好友请求");
                if(!findTextClick(FBStringUtil.getString(StringConst.QUEREN))){
                    NLogger.d(TAG,"没有找到有确认的点击按钮 表示无可添加通过的好友请求");
                    stopTask();
                }
                beginAddFriendAdopt();
            }
        }else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.2Bh")){ //弹出对话框自动点击确认
            // mService.execShellCmd("input tap 197 141"); //随便模拟一次点击 消失对话框
            if(findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEDING))){
            }else {
                findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEREN));
            }
        }
    }


    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendAdopt(){
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if(scrllorNodeInfos.size() > 1){
                    scrllorNodeInfo = scrllorNodeInfos.get(1);
                }
                if(scrllorNodeInfo == null){
                    //ToastUtil.showToast(AppContext.getInstance().getContext(),"scrllorNodeInfo listView null");
                    return;
                }
                if(scrllorNodeInfo.getChildCount() == 0){
                    NLogger.d(TAG,"没有找到有确认的点击按钮");
                    stopTask();
                    return;
                }
                for(int q=0;q<scrllorNodeInfo.getChildCount();q++){

                    if(currentAddCount  == maxAddCount){
                        currentAddCount = 0;
                        stopTask();
                        //停止任务
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    List<AccessibilityNodeInfo> list = as.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.QUEREN));
                    //没有找到确认按钮 直接停止结束任务
                    if(!list.isEmpty()){
                        for(AccessibilityNodeInfo accessibilityNodeInfo:list){
                            sleepTimedDefault();
                            currentAddCount++;
                            NLogger.d(TAG,"点击了确认"+   NodeUtil.performClick(accessibilityNodeInfo));
                            break;
                        }
                    }
                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime){
       // stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;

        initFbMain = false;
        init_imm =false;
        initRefresh =false;
    }


}
